<?php
/**
 * 商品属性模型
 */
class GoodsInfoModel extends Model{
	public $table = 'goods_info';
		public $validate = array();
		
	//添加商品详情表	
	public function addGoodsInfo(){
		$this->validate = array(
			  array('goods_info_detail', 'nonull', ' 商品详情不能为空',2,3),
			  array('goods_info_detail', 'nonull', ' 商品详情不能为空',2,3),
		);
		if(!$this->create()) return false;	
/*			//如果异步上传了图片就不执行上传  没有就执行表单上传
				//缩略图处理  如果有图片就上传
				if($_FILES['goodsImg']['error'][0] != 4){
					$upload = new Upload('upload/goods/'. date('y/m/d'));
					$thumb = new WaterMark();			
					$file = $upload->upload();
//					p($file);die;
					foreach($file as $k => $v){
						//缩略图处理 大中小图
						$smallImg[$k] = $thumb->ThumImg($v['path'],200,200,'yes','./upload/goods/small',$v['filename']);
						$bigImg[$k] = $thumb->ThumImg($v['path'],800,800,'yes','./upload/goods/big',$v['filename']);
						$middlImg[$k] = $thumb->ThumImg($v['path'],400,400,'yes','./upload/goods/middle',$v['filename']);
						$data = $file[0];
						//返回数据用    可用于插入数据库  介于 组合路径可提取文件名 组合 不做插入
						//$data['s_thumb'] = $smallImg;	
						//$data['b_thumb'] = $bigImg;	
						//$data['m_thumb'] = $middlImg;
						//记录数据 插入数据库
						//原图路径形式  http://localhost/0423/suning/upload/goods/15/06/25/19231435235850.jpg  未作删除
						//缩略图路径形式  只取文件名进行保存 
						//  [s_thumb] => Array
						//      (
						//          [0] => 	./upload/goods/small/90151435240047_thumb_200X200.jpeg
						//          [1] =>  ./upload/goods/middle/90151435240047_thumb_400X400.jpeg
						//          [2] =>  ./upload/goods/big/90151435240047_thumb_800X800.jpeg
						//      )
						//记录上传图片路径  一张图 对应三张缩略图 只取一个文件名即可  调用是 组合路径即可
						$imgurl[] = $v['filename'];
					}
						//将文件名组合成字符串','号分隔开 直接插入数据库
						$imgurl = implode(',', $imgurl);
						//大中小选择一插入字段信息 只用插入一次
						$data['goods_info_smallIMg'] = $imgurl;


				}*/
				//小图  如果异步上传图像  比表谈提交有值  直接复制插入
			if(Q('post.goods_info_smallIMg') !== ''){
				$data['goods_info_smallIMg'] = Q('post.goods_info_smallIMg');
			}
			//组合信息  商品详情表 goodsInfo
			$data['goods_info_detail'] = $_POST['goods_info_detail'];  //商品详情			
			$data['goods_info_sever'] = $_POST['goods_info_sever'];  //售后服务
			$data['goods_gid'] = $_SESSION['goods_gid'];  //商品id
			//将组合数据插入数据库
			$this->add($data);


			return true;		
			
	}
	//修改商品详情表	
	public function editGoodsInfo($gid){
		$this->validate = array(
			  array('goods_info_detail', 'nonull', ' 商品详情不能为空',2,3),
			  array('goods_info_detail', 'nonull', ' 商品详情不能为空',2,3),
		);
		if(!$this->create()) return false;	
			//如果异步上传了图片就不执行上传  没有就执行表单上传
				//缩略图处理  如果有图片就上传
				if($_FILES['goodsImg']['error'][0] != 4){
					$upload = new Upload('upload/goods/'. date('y/m/d'));
					$thumb = new WaterMark();			
					$file = $upload->upload();
//					p($file);die;
					foreach($file as $k => $v){
						//缩略图处理 大中小图
						$smallImg[$k] = $thumb->ThumImg($v['path'],200,200,'yes','./upload/goods/small',$v['filename']);
						$bigImg[$k] = $thumb->ThumImg($v['path'],800,800,'yes','./upload/goods/big',$v['filename']);
						$middlImg[$k] = $thumb->ThumImg($v['path'],400,400,'yes','./upload/goods/middle',$v['filename']);
						$data = $file[0];
						$imgurl[] = $v['filename'];
					}
						//将文件名组合成字符串','号分隔开 直接插入数据库
						$imgurl = implode(',', $imgurl);
						//大中小选择一插入字段信息 只用插入一次
						$data['goods_info_smallIMg'] = $imgurl;						
	
				}
				//小图  如果异步上传图像  读取隐藏表单值 插入数据库
			if(Q('post.goods_info_smallIMg') !== ''){  
				$data['goods_info_smallIMg'] = Q('post.goods_info_smallIMg');
			}else if(!isset($imgurl)  &&  Q('post.goods_info_smallIMg') == ''){ //如果都为空  赋值空 完成修改
				$data['goods_info_smallIMg'] = '';
			}

			//组合信息  商品详情表 goodsInfo
			$data['goods_info_detail'] = $_POST['goods_info_detail'];  //商品详情			
			$data['goods_info_sever'] = $_POST['goods_info_sever'];  //售后服务
			$data['goods_gid'] = $gid;  //商品id
//			var_dump($data);die;
			//将组合数据插入数据库
			$this->where("goods_gid ='{$gid}'")->update($data);

			return true;		
			
	}
}
